import {Component, OnInit} from '@angular/core';
import {Store} from "@ngxs/store";
import {LoginAction} from "./state/login.action";
import {WebsocketService} from "./core/websocket.service";
import {environment} from "../environments/environment";
import {RecognizeAction} from "./state/recognize.action";
import {FfdecWasmLoaderService} from "./ffdec-wasm-loader.service";
import {ActivatedRoute, Router} from "@angular/router";

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.less']
})
export class AppComponent implements OnInit {
  constructor(private store: Store, private ws: WebsocketService,
              private ffdecWasmLoaderService: FfdecWasmLoaderService, private router: Router, private route: ActivatedRoute) {
    this.ffdecWasmLoaderService.load();
  }

  ngOnInit(): void {
      //console.log('wsPath is :', localStorage.getItem('wsPath'));
    const heard = location.search;
    if (heard) {
      if (heard.concat('jwt')) {
        localStorage.setItem('Bearer', heard.split('=')[1]);
        this.store.dispatch(new LoginAction(heard.split('=')[1])).subscribe(() => {
          this.router.navigate(['/home']);
        });
      }
    }

    // this.route.queryParamMap.subscribe(paramMap => {
    //   console.log('进来了', paramMap.get('jwt'));
    //   if (paramMap.has('jwt')) {
    //     // const jwt = paramMap.get('jwt');
    //     localStorage.setItem('Bearer', paramMap.get('jwt'));
    //     this.store.dispatch(new LoginAction(jwt)).subscribe(() => {
    //       this.router.navigate(['/home']);
    //     });
    //   }
    // })
    const jwt = localStorage.getItem('Bearer');
    if (jwt) {
      this.store.dispatch(new LoginAction(jwt));
    }
   

 this.ws.connect(`${localStorage.getItem('wsPath')}/v1/detect-socket`).subscribe((data: string) => {
      console.log('data is :', data);
      this.store.dispatch(new RecognizeAction(data));
    });

/*
    this.ws.connect(`${environment.WS_URL}/v1/detect-socket`).subscribe((data: string) => {
      console.log('data is :', data);
      this.store.dispatch(new RecognizeAction(data));
    });
*/
  }
}
